package com.example.qsindusty.jmm_spline.InfomationFragment;


import android.app.Fragment;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AlertDialog;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.qsindusty.jmm_spline.Config.Config;
import com.example.qsindusty.jmm_spline.Jpush.MyReceiver;
import com.example.qsindusty.jmm_spline.OrderinfoActivity.OrderInfoActivity;
import com.example.qsindusty.jmm_spline.R;
import com.scwang.smartrefresh.header.MaterialHeader;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadmoreListener;
import com.yixin.ystartlibrary.base.BaseFragment;
import com.yixin.ystartlibrary.base.BaseRecyclerAdapter;
import com.yixin.ystartlibrary.utils.DensityUtil;
import com.yixin.ystartlibrary.utils.GeneralPreferencesUtils;
import com.yixin.ystartlibrary.utils.LoadingDialogUtils;
import com.yixin.ystartlibrary.utils.LogUtils;
import com.yixin.ystartlibrary.utils.ToastUtils;
import com.yixin.ystartlibrary.widget.DiverItemDecoration;
import com.yixin.ystartlibrary.widget.EmptyRecyclerView;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * A simple {@link Fragment} subclass.
 * Use the {@link InfomationFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class InfomationFragment extends BaseFragment implements InfoContarct.View,MyReceiver.Reveiverlister1 {

    @BindView(R.id.materialHeader)
    MaterialHeader materialHeader;
    @BindView(R.id.recyclerview)
    EmptyRecyclerView recyclerview;
    @BindView(R.id.iv_empty)
    ImageView ivEmpty;
    @BindView(R.id.empty_text)
    TextView emptyText;
    @BindView(R.id.refreshLayout)
    SmartRefreshLayout refreshLayout;
    Unbinder unbinder;
    String row = Config.row;//默认为10条
    String page = Config.page; //
    String status = "2";//默认为2未接货  未分配司机，已分配司机，未接货 运输中 已完成0-4
    InfoPresenter mPerenter ;
    OrderAdapter orderAdapter ;
    public  static  int MARK_LOCATION= 1000;
    public InfomationFragment() {
        // Required empty public constructor
    }

    public static InfomationFragment newInstance() {
        InfomationFragment fragment = new InfomationFragment();
        Bundle args = new Bundle();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_order_list, container, false);
        unbinder = ButterKnife.bind(this, view);
        return view;
    }

    public static boolean isFresh = false;
    @Override
    public void onResume() {
        super.onResume();
        if (isFresh) {
            page="1";
         //   getOrder();
            mPerenter.getorderinfo(GeneralPreferencesUtils.getSharedPreference(getActivity(),Config.token,null),row,page);
            isFresh = false;
        }
    }


    @Override
    protected int getContextView() {
        return 0;
    }

    @Override
    protected void init() {
        bindPresenter();
        initrefresh();
        initview();
        MyReceiver.setLister(this);
        String token = GeneralPreferencesUtils.getSharedPreference(getActivity(),Config.token,null);
        mPerenter.getorderinfo(token,row,page);
    }

    @Override
    protected void initBundleData() {

    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
        unbindPresenter();
    }
    private void initrefresh() {
        refreshLayout.setEnableScrollContentWhenLoaded(false);
        refreshLayout.setEnableAutoLoadmore(false);
        refreshLayout.setOnRefreshLoadmoreListener(new OnRefreshLoadmoreListener() {
            @Override
            public void onLoadmore(RefreshLayout refreshlayout) {

                mPerenter.getorderinfo(GeneralPreferencesUtils.getSharedPreference(getActivity(), "token", null),  row, page);
            }

            @Override
            public void onRefresh(RefreshLayout refreshlayout) {
                page = "1";
                LogUtils.e("initrefreshpage==" + page);
                mPerenter.getorderinfo(GeneralPreferencesUtils.getSharedPreference(getActivity(), "token", null), row, page);

            }
        });
    }
    private void initview() {
        orderAdapter = new OrderAdapter();
        recyclerview.setLayoutManager(new LinearLayoutManager(getActivity()));
        DiverItemDecoration itemDecoration = new DiverItemDecoration(DiverItemDecoration.VERTICAL);
        itemDecoration.setSize(DensityUtil.dip2px(getActivity(), 0.5f));
        itemDecoration.setColor(ContextCompat.getColor(getActivity(), R.color.default_layout_color));
        recyclerview.addItemDecoration(itemDecoration);
        //解决ScrollView嵌套RecyclerView滑动卡帧
        recyclerview.setNestedScrollingEnabled(false);
        //色通知
        emptyText.setText("点击重新加载");
        recyclerview.setEmptyView(LayoutInflater.from(getActivity()).inflate(R.layout.item_emptyview, null));
        recyclerview.setAdapter(orderAdapter);
    }


    AlertDialog alertDialog ;
    @Override
    public void showLoading() {
        alertDialog =  LoadingDialogUtils.create(getActivity(),"登陆");
        alertDialog.show();

    }

    @Override
    public void closeLoading() {
        if (alertDialog!=null&&alertDialog.isShowing())
            alertDialog.dismiss();
    }

    @Override
    public void bindPresenter() {
        if (mPerenter == null) {
            mPerenter = new InfoPresenter();
        }
        mPerenter.bindView(this);
    }

    @Override
    public void unbindPresenter() {
        mPerenter.unbindView();
    }

    @Override
    public void getSuccessful(InfoData infoData) {
        refreshLayout.finishRefresh();
        refreshLayout.finishLoadmore();
        if (page.equals("1")) {
            orderAdapter.clearInfoList();
            orderAdapter.addInfoList(infoData.getData());
        } else {
            orderAdapter.addInfoList(infoData.getData());
        }
        int p = Integer.parseInt(page);
        page = String.valueOf(p + 1);
        orderAdapter.notifyDataSetChanged();
    }

    @Override
    public void Frailer(String msg) {
        refreshLayout.finishRefresh();
        refreshLayout.finishLoadmore();
        ToastUtils.showLongToast(getActivity(),msg);
    }

    @Override
    public void adddarge() {
        String token = GeneralPreferencesUtils.getSharedPreference(getActivity(),Config.token,null);
        page="1";
        mPerenter.getorderinfo(token,row,page);
    }

    public class OrderAdapter extends BaseRecyclerAdapter<InfoData.DataBean, OrderAdapter.ViewHolder> {
        @Override
        protected void onBindViewHolder(int position, OrderAdapter.ViewHolder holder) {
            InfoData.DataBean dataBean = getItem(position);
            holder.setvauels(dataBean);
        }

        @Override
        protected OrderAdapter.ViewHolder getViewHolder(ViewGroup parent, int viewType) {
            return new


                    OrderAdapter.ViewHolder(inflate(R.layout.item_infomation, parent));

        }

        @Override
        protected void onBindItemClickListener(View v, int position) {
            super.onBindItemClickListener(v, position);

            Intent intent = new Intent(getActivity(), OrderInfoActivity.class);
            intent.putExtra(Config.data,getItem(position).getDH());

            intent.putExtra(Config.flags,"10");
          //   startActivity(intent);
        startActivityForResult(intent, MARK_LOCATION);
        }

        class ViewHolder extends RecyclerView.ViewHolder {
            @BindView(R.id.ornum)
            TextView ornum;
            @BindView(R.id.driver_bottom)
            TextView driverBottom;
            @BindView(R.id.order_status)
            TextView orderStatus;
            @BindView(R.id.info_time)
            TextView infoTime;
            @BindView(R.id.infomations)
            TextView infomations;
            ViewHolder(View view) {
                super(view);
                ButterKnife.bind(this, view);
            }

            void setvauels(InfoData.DataBean dataBean) {
                if (!TextUtils.isEmpty(dataBean.getDH()))
                    ornum.setText("订单号："+dataBean.getDH());
                if (!TextUtils.isEmpty(dataBean.getORDERZT())){
                    if (TextUtils.equals(dataBean.getORDERZT(),"0")){
                            driverBottom.setText("新订单");
                    }
                    if (TextUtils.equals(dataBean.getORDERZT(),"1")){
                        driverBottom.setText("已分配");
                    }
                    if (TextUtils.equals(dataBean.getORDERZT(),"2")){
                        driverBottom.setText("未接货");
                    }
                    if (TextUtils.equals(dataBean.getORDERZT(),"3")){
                        driverBottom.setText("运输中");

                    }
                    if (TextUtils.equals(dataBean.getORDERZT(),"4")){
                        driverBottom.setText("已完成");
                    }
                    }

                    if (!TextUtils.isEmpty(dataBean.getXGSJ())){
                        infoTime.setText("系统时间："+dataBean.getXGSJ());
                    }
                    if (!TextUtils.isEmpty(dataBean.getMSG()))
                        infomations.setText("系统内容："+dataBean.getMSG());
                }

            }
    }

    }


